package com.example.translateappjava.Utils;

import android.os.Parcel;
import android.os.Parcelable;
import androidx.annotation.NonNull;
import androidx.room.Entity;
import androidx.room.Ignore;
import androidx.room.PrimaryKey;
import com.google.gson.annotations.SerializedName;

@Entity(tableName = "posts")
public class Post implements Parcelable {

    @PrimaryKey(autoGenerate = true)
    @NonNull
    private Long localId; // 本地数据库主键，自动生成

    @SerializedName("id") // JSON中的 "id" 对应服务器的ID
    private Long serverId; // 服务器生成的ID，可能为null（如新帖子发送前）

    @SerializedName("userName")
    private String username;

    @SerializedName("title")
    private String title;

    @SerializedName("content")
    private String content;

    @SerializedName("category")
    private String category;

    @SerializedName("createdAt")
    private String time; // 服务器返回的时间戳 (通常是ISO 8601字符串)

    @SerializedName("likeCount")
    private int likeCount;

    @SerializedName("commentCount")
    private int commentCount;

    @SerializedName("avatarUrl")
    private String avatarUrl;

    @SerializedName("isLikedByCurrentUser")
    private Boolean isLikedByCurrentUser;

    @SerializedName("isFavoritedByUser")
    private Boolean isFavoritedByUser;

    // Room 使用的无参构造函数
    public Post() {
    }

    // GSON 和我们自己代码中可能使用的构造函数 (serverId可能为null)
    // Room 会忽略这个，因为它有无参构造函数
    @Ignore
    public Post(Long serverId, String username, String title, String content, String category, String time,
                int likeCount, int commentCount, String avatarUrl,
                Boolean isLikedByCurrentUser, Boolean isFavoritedByUser) {
        this.serverId = serverId;
        this.username = username;
        this.title = title;
        this.content = content;
        this.category = category;
        this.time = time;
        this.likeCount = likeCount;
        this.commentCount = commentCount;
        this.avatarUrl = avatarUrl;
        this.isLikedByCurrentUser = isLikedByCurrentUser;
        this.isFavoritedByUser = isFavoritedByUser;
    }

    // Parcelable 构造函数
    @Ignore // 通常Room不使用这个，但明确标记一下无妨
    protected Post(Parcel in) {
        if (in.readByte() == 0) {
            localId = null;
        } else {
            localId = in.readLong();
        }
        if (in.readByte() == 0) {
            serverId = null;
        } else {
            serverId = in.readLong();
        }
        username = in.readString();
        title = in.readString();
        content = in.readString();
        category = in.readString();
        time = in.readString();
        likeCount = in.readInt();       // 确保顺序正确
        commentCount = in.readInt();    // 确保顺序正确
        avatarUrl = in.readString();
        byte tmpIsLiked = in.readByte();
        isLikedByCurrentUser = tmpIsLiked == 0 ? null : tmpIsLiked == 1;
        byte tmpIsFavorited = in.readByte();
        isFavoritedByUser = tmpIsFavorited == 0 ? null : tmpIsFavorited == 1;
    }

    @Override
    public void writeToParcel(@NonNull Parcel dest, int flags) {
        if (localId == null) {
            dest.writeByte((byte) 0);
        } else {
            dest.writeByte((byte) 1);
            dest.writeLong(localId);
        }
        if (serverId == null) {
            dest.writeByte((byte) 0);
        } else {
            dest.writeByte((byte) 1);
            dest.writeLong(serverId);
        }
        dest.writeString(username);
        dest.writeString(title);
        dest.writeString(content);
        dest.writeString(category);
        dest.writeString(time);
        dest.writeInt(likeCount);       // 确保顺序正确
        dest.writeInt(commentCount);    // 确保顺序正确
        dest.writeString(avatarUrl);
        dest.writeByte((byte) (isLikedByCurrentUser == null ? 0 : isLikedByCurrentUser ? 1 : 2));
        dest.writeByte((byte) (isFavoritedByUser == null ? 0 : isFavoritedByUser ? 1 : 2));
    }

    public static final Creator<Post> CREATOR = new Creator<Post>() {
        @Override
        public Post createFromParcel(Parcel in) {
            return new Post(in);
        }

        @Override
        public Post[] newArray(int size) {
            return new Post[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    // --- Getters and Setters ---
    @NonNull
    public Long getLocalId() {
        return localId;
    }

    public void setLocalId(@NonNull Long localId) {
        this.localId = localId;
    }

    public Long getServerId() {
        return serverId;
    }

    public void setServerId(Long serverId) {
        this.serverId = serverId;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public int getLikeCount() {
        return likeCount;
    }

    public void setLikeCount(int likeCount) {
        this.likeCount = likeCount;
    }

    public int getCommentCount() {
        return commentCount;
    }

    public void setCommentCount(int commentCount) {
        this.commentCount = commentCount;
    }

    public String getAvatarUrl() {
        return avatarUrl;
    }

    public void setAvatarUrl(String avatarUrl) {
        this.avatarUrl = avatarUrl;
    }

    public Boolean getIsLikedByCurrentUser() {
        return isLikedByCurrentUser;
    }

    public void setIsLikedByCurrentUser(Boolean likedByCurrentUser) {
        isLikedByCurrentUser = likedByCurrentUser;
    }

    public Boolean getIsFavoritedByUser() {
        return isFavoritedByUser;
    }

    public void setIsFavoritedByUser(Boolean favoritedByUser) {
        isFavoritedByUser = favoritedByUser;
    }
}